﻿#include "u_quailty_sim.h"

SimHistory::SimHistory(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    /*this->SetupConnections()*/;
    this->Initialize();
    this->updateTable();
    this->setTitle("模拟历史");
    this->resize(1500, 700);
    this->setModal(false);
    connect(btnQuery,SIGNAL(pressed()),this,SLOT(update()));
}

void SimHistory::update()
{
    this->Initialize();
    this->updateTable();
}

void SimHistory::InitWidget(){
    btnQuery  = new QPushButton(QChar(0xe639) + QString(" 查询"));

    QList<Equipment> equList = globalData->sqlBase->GetEquipmentList();
    editEquCode = new QComboBox();
    for(int i = 0; i<equList.size(); i++){
        Equipment equ = equList.at(i);
        editEquCode->addItem(equ.devname);
    }

    editStartTime   = new QDateTimeEdit();  editStartTime->setDisplayFormat("yyyy-MM-dd HH:mm:ss"); editStartTime->setCalendarPopup(true);/*弹出日历窗口，当存在有效的日期显示格式时，此属性才有效。*/  editStartTime->setDateTime(QDateTime::currentDateTime().addSecs(-20));//比当前时间早7.2秒
    editEndTime     = new QDateTimeEdit();    editEndTime->setDisplayFormat("yyyy-MM-dd HH:mm:ss"); editEndTime->setCalendarPopup(true);   editEndTime->setDateTime(QDateTime::currentDateTime());

    tableWidget1 =new QTableWidget();
    tableWidget1->setColumnCount(15);
    tableWidget1->setHorizontalHeaderLabels(QStringList()<<"设备"<<"时间"<<"入射功率"<<"反射功率"
                                            <<"高频采样"<<"-140V"<<"-18V"<<"12V"<<"18V"
                                            <<"24V"<<"音频信号"<<"中放电流"<<"调制电流"<<"推动电流"<<"频率");
//    tableWidget1->horizontalHeader()->setStretchLastSection(true);
//    tableWidget1->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    for(int i=0;i<tableWidget1->currentColumn();i++)
    {
        tableWidget1->horizontalHeader()->setSectionResizeMode(i,QHeaderView::ResizeToContents);
    }
    tableWidget1->setColumnWidth(1,170);
    tableWidget1->setAlternatingRowColors(true);

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addWidget(new QLabel("设备:"));
    btnLayout->addWidget(editEquCode);
    btnLayout->addWidget(new QLabel("开始时间:"));
    btnLayout->addWidget(editStartTime);
    btnLayout->addWidget(new QLabel("结束时间:"));
    btnLayout->addWidget(editEndTime);
    btnLayout->addStretch();
    btnLayout->addWidget(btnQuery);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(btnLayout);

    //-------------------------------------------------02 0F 12 1B 21 2A

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(groupbox11);
    mainLayout->addWidget(tableWidget1);
    this->addLayout(mainLayout);
}

void SimHistory::Initialize()
{
    QString name=editEquCode->currentText();
    QDateTime tr=editStartTime->dateTime();
    QDateTime et=editEndTime->dateTime();

    SimuList=globalData->sqlBase->GetSimList(name,tr,et);
}

void SimHistory::updateTable()
{
    int count = SimuList.size();

    tableWidget1->setRowCount(0);
    tableWidget1->setRowCount(count);

    for(int i=0; i<count; i++){
        Simulation info = SimuList.at(i);

        tableWidget1->setItem(i, 0, MyGUI::GetTableWidgetItem(info.devname));
        tableWidget1->setItem(i, 1, MyGUI::GetTableWidgetItem(info.datetime));
        tableWidget1->setItem(i, 2, MyGUI::GetTableWidgetItem(info.launch));
        tableWidget1->setItem(i, 3, MyGUI::GetTableWidgetItem(info.reflect));
        tableWidget1->setItem(i, 4, MyGUI::GetTableWidgetItem(info.highFreq));
        tableWidget1->setItem(i, 5, MyGUI::GetTableWidgetItem(info.FOFZ));
        tableWidget1->setItem(i, 6, MyGUI::GetTableWidgetItem(info.FOE));
        tableWidget1->setItem(i, 7, MyGUI::GetTableWidgetItem(info.OT));
        tableWidget1->setItem(i, 8, MyGUI::GetTableWidgetItem(info.OE));
        tableWidget1->setItem(i, 9, MyGUI::GetTableWidgetItem(info.TF));
        tableWidget1->setItem(i, 10, MyGUI::GetTableWidgetItem(info.audio));
        tableWidget1->setItem(i, 11, MyGUI::GetTableWidgetItem(info.putting));
        tableWidget1->setItem(i, 12, MyGUI::GetTableWidgetItem(info.modulate));
        tableWidget1->setItem(i, 13, MyGUI::GetTableWidgetItem(info.drive));
        tableWidget1->setItem(i, 14, MyGUI::GetTableWidgetItem(info.fre));
    }
}
